package com.xxjd;

import com.xxjd.config.SpringBeanNameGenerator;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;

/**
 * 启动类
 *
 * @author honghh
 * Date 2019-10-12 10:55:44
 * Copyright (C) www.honghh.top
 */
//开启事务
@EnableTransactionManagement
@ServletComponentScan
@SpringBootApplication
@MapperScan(value = {"com.xxjd.dao"}, nameGenerator = SpringBeanNameGenerator.class)
//扫描包com.xxjd，自定义BeanName生成策略, 解决不同包下同名类问题
@ComponentScan(basePackages = {"com.xxjd"}, nameGenerator = SpringBeanNameGenerator.class)
@EnableAsync   //开启异步注解功能
public class WebApplication extends SpringBootServletInitializer {

    private static Logger logger = LoggerFactory.getLogger(WebApplication.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(WebApplication.class, args);
        printSystemInfo(applicationContext);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

    private static void printSystemInfo(ApplicationContext applicationContext) {
        Environment environment = applicationContext.getEnvironment();
        String activeProfiles = environment.getProperty("spring.profiles.active");
        if (StringUtils.isEmpty(activeProfiles) || !activeProfiles.contains("dev")) {
            return;
        }
        String port = environment.getProperty("server.port");
        String context = environment.getProperty("server.servlet.context-path");
        String ip = "localhost";
        String systemIndexUrl = "http://" + ip + ":" + port + context;
        String swaggerBootStrapApiUrl = systemIndexUrl + "doc.html";
        System.out.printf(">>>>>>>>>>>>>>>>>>>>接口文档地址2(bootstrap美化过,推荐使用)：%s >>>>>>>>>\n", swaggerBootStrapApiUrl);
        String druidAccessUrl = systemIndexUrl + "druid";
        System.out.printf(">>>>>>>>>>>>>>>>>>>>druid数据监控中心访问url：%s >>>>>>>>>\n", druidAccessUrl);
    }

}
